package org.example.practice.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.domain.R;
import org.example.commonapi.util.JwtUtil;
import org.example.practice.service.CollectPracticeService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/practice")
@Slf4j
public class UserPracticeController {

    @Resource
    CollectPracticeService collectPracticeService;

    @Resource
    JwtUtil jwtUtil;

    /**
     * 收藏题目
     * @param request
     * @return
     */
    @PostMapping("/collect/{id}")
    public R collect(@PathVariable("id")Long id, HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        collectPracticeService.collect(loginInfo.getId(),id);
        log.info("用户{}收藏题目:{}",loginInfo.getId(),id);
        return R.success();
    }

}
